package com.squirrel.michale.string;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * TODO function description
 *
 * @author m30026548
 * @since 2025-10-07
 */

/*
*
*
* 【软件认证】公共字符

给定一组字符串strings，请计算有哪些字符在所有字符串中都出现过 count 次及以上。

输入
第一个参数是count，1 <= count <= 100
第二个参数是strings，1 <= strings.length <= 100，1 <= strings[i].length < 1000，strings[i] 仅由英文字母和数字组成

输出
按ASCII码升序输出所有符合要求的字符； 如果没有符合要求的字符，则输出空列表[]。

样例1
复制输入：
2
["aabbccFFFFx2x2", "aaccddFFFFx2x2", "aabcdFFFFx2x2"]
复制输出：
["2", "F", "a", "x"]
解释：
字符 a 在三个字符串中都出现 2次，符合要求；
字符 b 在第二三个字符串中分别出现 0次、1次，不符合要求；
字符 c 在第三个字符串中出现 1次，不符合要求；
字符 d 在第三个字符串中出现 1次，不符合要求；
字符 F 在三个字符串中都出现了 4 次，符合要求；
字符 x 在三个字符串中都出现了 2 次，符合要求；
字符 2 在三个字符串中都出现了 2 次，符合要求；

因此字符 a、F、x、2符合要求，按ASCII码升序输出

样例2
复制输入：
2
["aa", "bb", "cc"]
复制输出：
[]
解释：
* */

public class Exercise001 {

    public char[] commonCharsV1(int count, List<String> strings) {

        Map<String, Map<Character, Integer>> totalMap = new HashMap<>();
        for (String single : strings) {
            char[] charArray = single.toCharArray();
            Map<Character, Integer> charFrequencMap = new HashMap<>();
            for (char c : charArray) {
                charFrequencMap.put(c, charFrequencMap.get(c) + 1);
            }
            Iterator<Map.Entry<Character, Integer>> iterator = charFrequencMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Character, Integer> entry = iterator.next();
                if (entry.getValue() < count) {
                    iterator.remove();
                }
            }
            totalMap.put(single, charFrequencMap);
        }

        return new char[]{};
    }

    public char[] commonCharsV2(int count, List<String> strings) {

        List<Map<Character, Integer>> totalMap = new ArrayList<>();
        for (String single : strings) {
            char[] charArray = single.toCharArray();
            Map<Character, Integer> charFrequencMap = new HashMap<>();
            for (char c : charArray) {
                if (charFrequencMap.get(c) == null) {
                    charFrequencMap.put(c, 1);
                }else{
                    charFrequencMap.put(c, charFrequencMap.get(c) + 1);
                }
            }
            charFrequencMap.entrySet().removeIf(entry -> entry.getValue() < count);
            totalMap.add(charFrequencMap);
        }

        Set<Character> result = totalMap.get(0).keySet();
        for (int i = 1; i < totalMap.size(); i++) {
            Set<Character> characters1 = totalMap.get(i).keySet();
            Iterator<Character> iterator = result.iterator();
            while (iterator.hasNext()) {
                Character next = iterator.next();
                if (!characters1.contains(next)) {
                    iterator.remove();
                }
            }

        }

        if (result.isEmpty()) {
            return new char[]{};
        }else{
            List<Character> characterList = new ArrayList<>(result);
            Collections.sort(characterList);
            Character[] array = characterList.toArray(new Character[0]);
            char[] real = new char[array.length];
            for (int i = 0; i < array.length; i++) {
                real[i] = array[i];
            }
            return real;

        }
    }

    public char[] commonChars(int count, List<String> strings) {

        List<Map<Character, Integer>> totalMap = new ArrayList<>();
        for (String single : strings) {
            char[] charArray = single.toCharArray();
            Map<Character, Integer> charFrequencMap = new HashMap<>();
            for (char c : charArray) {
                charFrequencMap.merge(c, 1, Integer::sum);
            }
            charFrequencMap.entrySet().removeIf(entry -> entry.getValue() < count);
            totalMap.add(charFrequencMap);
        }

        Set<Character> result = totalMap.get(0).keySet();
        for (int i = 1; i < totalMap.size(); i++) {
            Set<Character> characters1 = totalMap.get(i).keySet();
            Iterator<Character> iterator = result.iterator();
            while (iterator.hasNext()) {
                Character next = iterator.next();
                if (!characters1.contains(next)) {
                    iterator.remove();
                }
            }

        }

        if (result.isEmpty()) {
            return new char[]{};
        }else{
            List<Character> characterList = new ArrayList<>(result);
            Collections.sort(characterList);
            Character[] array = characterList.toArray(new Character[0]);
            char[] real = new char[array.length];
            for (int i = 0; i < array.length; i++) {
                real[i] = array[i];
            }
            return real;

        }
    }

    public static void main(String[] args) {
        Exercise001 exercise002 = new Exercise001();
        List<String> stringList = new ArrayList<>();
        stringList.add("aabbccFFFFx2x2");
        stringList.add("aaccddFFFFx2x2");
        stringList.add("aabcdFFFFx2x2");
        char[] chars = exercise002.commonChars(2, stringList);
        System.out.println(Arrays.toString(chars));


        List<String> stringList2 = new ArrayList<>();
        stringList2.add("aa");
        stringList2.add("bb");
        stringList2.add("cc");
        char[] chars2 = exercise002.commonChars(2, stringList2);
        System.out.println(Arrays.toString(chars2));
    }


    private static char[] getNTimesCharacter(int nValue, String[] strings) {
        char[] chars = strings[0].toCharArray();
        List<Character> list = new ArrayList<>();
        for (char c : chars) {
            if (!list.contains(c)) {
                list.add(c);
            }
        }
        // 排序
        Collections.sort(list);
        List<String> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String cList = String.valueOf(list.get(i));
            boolean isTrue = true;
            for (int j = 0; j < strings.length; j++) {
                if (strings[j].length() - strings[j].replace(cList, "").length() < nValue) {
                    isTrue = false;
                }
            }
            if (isTrue) {
                result.add(cList);
            }
        }
        char[] cha = new char[result.size()];
        for (int i = 0; i < result.size(); i++) {
            cha[i] = result.get(i).charAt(0);
        }
        return cha;
    }

    public char[] commonCharsV4(int count, List<String> strings) {
        List<Map<Character, Integer>> totalMap = new ArrayList<Map<Character, Integer>>();
        for (String string : strings) {
            char[] charArray = string.toCharArray();
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            for (char c : charArray) {
                map.put(c, map.getOrDefault(c, 0) + 1);
            }
            map.entrySet().removeIf(entry -> entry.getValue() < count);
            totalMap.add(map);
        }

        Set<Character> resultSet = totalMap.get(0).keySet();
        for (int i = 1; i < totalMap.size(); i++) {
            Set<Character> characters1 = totalMap.get(i).keySet();
            Iterator<Character> iterator = resultSet.iterator();
            while (iterator.hasNext()) {
                Character next = iterator.next();
                if (!characters1.contains(next)) {
                    iterator.remove();
                }
            }
        }

        Character[] array = resultSet.toArray(Character[]::new);
        Arrays.sort(array);
        char[] charArray = new char[array.length];
        for (int i = 0; i < array.length; i++) {
            charArray[i] = array[i];
        }

        return charArray;
    }
}
